﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmUpdateAge

    Dim _mysql As New ClsSQLhelper
    Dim Strsql As String
    Dim on_load As Boolean = True
    Public Primary_Code As String = ""
    Dim DT_Type As New DataTable
#Region "Function"
    Sub Load_Class(Optional ByVal Cretiria As String = "")
        Try
            Strsql = "Select Type_ID,Type_Name,Type_Desc,Section from typeitem where Type_ID like '" & Cretiria & "%'"
            Dim StrCode As String = ""

            DT_Type = _mysql.GetMYSQLDataTable(Strsql, "typeitem")
            With GridType
                .DataSource = DT_Type

            End With
            Me.GridColumn1.FieldName = "Type_ID"
            Me.GridColumn2.FieldName = "Section"
            Me.GridColumn3.FieldName = "Type_Name"
            Me.GridColumn4.FieldName = "Type_Desc"

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Class"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
#End Region
    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Dim Gv As GridView = sender
        Dim CodeName As String

        Primary_Code = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Type_ID")).ToString()
        BtnUpdateUsageTime_Click(sender, e)

    End Sub



    Private Sub BtnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSearch.Click
        Me.Primary_Code = ""
        Load_Class(Me.TxtCode.Text)
    End Sub

    Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
        Me.Dispose()
    End Sub

    Private Sub GridType_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridType.Click

    End Sub

    Private Sub BtnUpdateUsageTime_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdateUsageTime.Click
        MsgBox("คำเตือนการปรับอายุการใช้งานในส่วนนี้ไม่สามารถยกเลิกได้ กรุณาตรวจสอบข้อมูลก่อนทำการลบทุกครั้งและใช้ในกรณีจำเป็นเท่านี้น", MsgBoxStyle.Exclamation, "คำเตือน โปรดอ่าน")
        If MsgBox("คุณต้องการปรับปรุงข้อมูลครุภัณฑ์ที่มีหมายเลข " & Primary_Code & " ใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการปรับปรุง") = MsgBoxResult.No Then Exit Sub
        Try
            Strsql = "Update typeitem set usagetime = " & Me.NudAge.EditValue & " where Type_ID = '" & Primary_Code & "'"
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "BtnUpdatePercentLost_Click"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = Strsql
                    frmDebug.ShowDialog()
                Case Else
                    '        MsgBox("ปรับปรุงข้อมูลเรียบร้อยแล้ว")
            End Select

            Strsql = "Update items set usagetime = " & Me.NudAge.EditValue & " where Item_Type = '" & Primary_Code & "'"
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    frmDebug.lblFormName.Text = Me.Name
                    frmDebug.lblFunctionName.Text = "BtnUpdateUsageTime_Click"
                    frmDebug.MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                    frmDebug.MemoSQL.Text = Strsql
                    frmDebug.ShowDialog()
                Case Else
                    MsgBox("ปรับปรุงข้อมูลเรียบร้อยแล้ว")
            End Select

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub TxtCode_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtCode.KeyDown
        If e.KeyCode = Keys.Enter Then
            If Me.Primary_Code = "" Then
                Primary_Code = DT_Type.Rows(0).Item("Type_ID").ToString.Trim
            End If
            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()
        End If
    End Sub

    Private Sub TxtCode_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCode.TextChanged
        Me.Primary_Code = ""
        Load_Class(Me.TxtCode.Text)
    End Sub
End Class